package com.herui.iot.ui.evaluation;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.herui.sdyu_lib.adapter.recycle_view.base.ViewHolder;
import com.herui.sdyu_lib.utils.ToastUtil;
import com.herui.sport_care.R;
import com.herui.sport_care.data.eval.AgreementInfo;
import com.herui.sport_care.data.eval.BetaSchemeInfo;
import com.herui.sport_care.data.eval.EvalHostoryInfo;
import com.herui.sport_care.data.eval.OrganTreeInfo;
import com.herui.sport_care.request.SortType;
import com.herui.sport_care.ui.BaseListSportFragment;
import com.herui.sport_care.ui.dialog.PopDialog;

import java.util.LinkedList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 评估历史
 */
public class EvalHistoryFragment extends BaseListSportFragment<EvalHostoryInfo> {

    private OrganTreeInfo organTreeInfo;
    private AgreementInfo agreementInfo;           // 评估方案所用测试协议id
    private String beEvaluatedName;                 // 被评估人姓名
    private BetaSchemeInfo betaSchemeInfo;          // 评估方案所用测试方案id
    private SortType sortType = SortType.ASC;   // 评估日期查询排序，默认倒序排列，SortType此类有两个值： ASC正序排列,DESC倒序排列;

    @BindView(R.id.id_recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    @BindView(R.id.view_select_organ)
    View view_select_organ;
    @BindView(R.id.view_select_beta_scheme)
    View view_select_beta_scheme;
    @BindView(R.id.view_select_agreement)
    View view_select_agreement;
    @BindView(R.id.view_select_date)
    View view_select_date;

    @BindView(R.id.tv_date_time)
    TextView dateTime;
    @BindView(R.id.tv_group)
    TextView group;
    @BindView(R.id.tv_programme)
    TextView programme;
    @BindView(R.id.tv_agreement)
    TextView agreement;
    @BindView(R.id.edit_name)
    EditText edit_name;

    private EvalPresenter evalPresenter;

    public EvalHistoryFragment() {
        evalPresenter = new EvalPresenter(view);
    }

    public static EvalHistoryFragment newInstance() {
        EvalHistoryFragment fragment = new EvalHistoryFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public int getLayout() {
        return R.layout.fragment_eval_history;
    }

    @Override
    public void initParam() {
        initList(recyclerView, R.layout.item_home_list, true, null);
        enablePullRefresh(swipeRefreshLayout);

        edit_name.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH ) {
                    String s = edit_name.getText().toString();
                    beEvaluatedName = s;
                    refresh();
                    return true;
                } return false;
            }
        });
    }

    @OnClick({R.id.view_select_organ, R.id.view_select_agreement, R.id.view_select_beta_scheme,
            R.id.view_select_date})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.view_select_date:
                sortType = sortType == SortType.ASC ? SortType.DESC : SortType.ASC;
                clearListData();
                doRefresh();
                break;
            case R.id.view_select_organ:
                evalPresenter.getOrganTreeList();
                break;
            case R.id.view_select_agreement:
                if (betaSchemeInfo == null) {
                    ToastUtil.showMessage("请先选择测试方案");
                    return;
                }
                evalPresenter.getAgreement(betaSchemeInfo.getId());
                break;
            case R.id.view_select_beta_scheme:
                evalPresenter.getBetaScheme();
                break;
            default:
                break;
        }

    }

    @Override
    protected void dataProcess(List<EvalHostoryInfo> datas) {
        String preDate = null;
        for (EvalHostoryInfo evalHostoryInfo : datas) {
            if (preDate == null || !preDate.equals(evalHostoryInfo.getEvaluationDate())) {
                evalHostoryInfo.setShowEvaluationDate(true);
                preDate = evalHostoryInfo.getEvaluationDate();
            } else {
                evalHostoryInfo.setShowEvaluationDate(false);
            }
        }
    }

    @Override
    protected void convert(ViewHolder holder, EvalHostoryInfo value, int position) {
        holder.setVisible(R.id.tv_eval_time, value.isShowEvaluationDate());
        if (value.isShowEvaluationDate()) {
            holder.setText(R.id.tv_eval_time, value.getEvaluationDate());
            holder.setVisible(R.id.v_eval_line_cut, true);
        } else {
            holder.setVisible(R.id.v_eval_line_cut, false);
        }
        holder.setText(R.id.tv_name, value.getName());
//        holder.setText(R.id.tv_agreement,"协议-" + value.getAgreementName());
        holder.setText(R.id.tv_time,
                value.getEvaluationDate() + "   " + value.getEvaluationStartTime());

        switch (value.getEvaluationStatus()) {
            case NOT_STARTED:
                holder.setText(R.id.tv_eval_status, "未开始");
                holder.setTextColor(R.id.tv_eval_status, 0xff7B7B7B);
                holder.setBackgroundRes(R.id.tv_eval_status, R.drawable.home_text_bg_1);
                holder.setBackgroundRes(R.id.view_item_bg, R.drawable.login_btn_bg_2);
                holder.setText(R.id.btn_item_eval, "开始评估");
                break;
            case IN_PROGRESS:
                holder.setText(R.id.tv_eval_status, "进行中");
                holder.setTextColor(R.id.tv_eval_status, 0xff63E6BE);
                holder.setBackgroundRes(R.id.tv_eval_status, R.drawable.home_text_bg_2);
                holder.setBackgroundRes(R.id.view_item_bg, R.drawable.login_btn_bg_1);
                holder.setText(R.id.btn_item_eval, "继续评估");
                break;
            case FINISHED:
                holder.setText(R.id.tv_eval_status, "已完成");
                holder.setTextColor(R.id.tv_eval_status, 0xff63E6BE);
                holder.setBackgroundRes(R.id.tv_eval_status, R.drawable.home_text_bg_2);
                holder.setBackgroundRes(R.id.view_item_bg, R.drawable.login_btn_bg_1);
                holder.setText(R.id.btn_item_eval, "查看详情");
                break;
            default:
                break;
        }
        holder.setOnClickListener(R.id.btn_item_eval, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(baseActivity, EvalDetailActivity.class);
                i.putExtra("evalId", value.getId().intValue());
                baseActivity.jump(i);
            }
        });


    }

    @Override
    protected void doRefresh() {
        Long organId = null;
        Long agreementId = null;
        Long betaSchemeId = null;
        if (organTreeInfo != null) {
            organId = organTreeInfo.getId();
        }
        if (agreementInfo != null) {
            agreementId = agreementInfo.getId();
        }
        if (betaSchemeInfo != null) {
            betaSchemeId = betaSchemeInfo.getId();
        }
        evalPresenter.getHistoryEval(getPageNumb(), organId, agreementId, beEvaluatedName,
                betaSchemeId, sortType);
    }

    EvalPresenter.View view = new EvalPresenter.View() {
        @Override
        public void onEvalHostoryInfo(List<EvalHostoryInfo> result) {
            addData(result);
        }

        @Override
        public void onOrganTreeInfo(OrganTreeInfo organTreeInfo) {

            List<String> collect = new LinkedList<>();
            List<OrganTreeInfo> childrenOrganList = organTreeInfo.getChildrenOrganList();
            for (OrganTreeInfo organTreeInfo1 : childrenOrganList) {
                collect.add(organTreeInfo1.getName());
            }

            new PopDialog(baseActivity, view_select_organ, R.layout.item_pop_eval_value, collect,
                    "", new PopDialog.SelectLis() {
                @Override
                public void onSelect(String value) {
                    for (OrganTreeInfo organTreeInfo1 : childrenOrganList) {
                        if (organTreeInfo1.getName().equals(value)) {
                            EvalHistoryFragment.this.organTreeInfo = organTreeInfo1;
                            group.setText(organTreeInfo1.getName() + "");
                            break;
                        }
                    }
                    clearListData();
                    doRefresh();
                }
            });
        }

        @Override
        public void onBetaSchemeInfo(List<BetaSchemeInfo> betaSchemeInfos) {
            List<String> collect = new LinkedList<>();
            for (BetaSchemeInfo betaSchemeInfo : betaSchemeInfos) {
                collect.add(betaSchemeInfo.getName());
            }

            new PopDialog(baseActivity, view_select_beta_scheme, R.layout.item_pop_eval_value,
                    collect, "", new PopDialog.SelectLis() {
                @Override
                public void onSelect(String value) {
                    for (BetaSchemeInfo betaSchemeInfo : betaSchemeInfos) {
                        if (betaSchemeInfo.getName().equals(value)) {
                            EvalHistoryFragment.this.betaSchemeInfo = betaSchemeInfo;
                            programme.setText(betaSchemeInfo.getName() + "");
                            break;
                        }
                    }
                    clearListData();
                    doRefresh();
                }
            });
        }

        @Override
        public void onAgreementInfo(List<AgreementInfo> agreementInfos) {
            List<String> collect = new LinkedList<>();
            for (AgreementInfo agreementInfo : agreementInfos) {
                collect.add(agreementInfo.getName());
            }

            new PopDialog(baseActivity, view_select_agreement, R.layout.item_pop_eval_value,
                    collect, "", new PopDialog.SelectLis() {
                @Override
                public void onSelect(String value) {
                    for (AgreementInfo agreementInfo : agreementInfos) {
                        if (agreementInfo.getName().equals(value)) {
                            EvalHistoryFragment.this.agreementInfo = agreementInfo;
                            agreement.setText(agreementInfo.getName() + "");
                            break;
                        }
                    }
                    clearListData();
                    doRefresh();
                }
            });
        }
    };
}